import { loginByPhone } from '@/api/login'
import { getToken,setToken } from '@/utils/auth'

const user = {
  state: {
    user: '',
    token: getToken()
  },

  mutations: {
    SET_TOKEN:(state , token)=>{
      state.token = token
    }

  },

  actions: {
    LoginByPhone({commit}, userInfo){
      const phone = userInfo.phone.trim()
      return new Promise((resolve, reject) => {
        loginByPhone(phone, userInfo.password).then(response => {
          const token = response.data.data
          commit('SET_TOKEN', token)
          setToken(token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default user
